﻿Imports Outlook = Microsoft.Office.Interop.Outlook
Imports System.IO
Imports Microsoft.VisualBasic.ControlChars
Imports System.Windows.Forms

Imports System.Xml.XPath
Imports System.Xml








Public Class toOutlook

    Dim PXMLclass As New PersonalXML

    Dim FileHandlerClass As New FileHandler

    Dim docNavigator As XPathNavigator
    Dim docNav As XPathDocument
    Dim NodeIter As XPathNodeIterator

    Dim blokNav As XPathDocument
    Dim blokNavigator As XPathNavigator


    Dim nodeDocent As XPathNodeIterator
    Dim nodeDag As XPathNodeIterator
    Dim nodeKlas As XPathNodeIterator
    Dim nodeVak As XPathNodeIterator
    Dim nodeLokaal As XPathNodeIterator
    Dim nodeLesuur As XPathNodeIterator
    Dim nodeWeken As XPathNodeIterator

    Dim Docent As String
    Dim Dag As String
    Dim Klas As String
    Dim Vak As String
    Dim Lokaal As String
    Dim Lesuur As String
    Dim Weken As String

    Dim blokRead As Boolean = False
    Dim tempText As String




    Public Sub XMLreader()

        Dim teller As Integer = 0
        Dim tempTarget As String = "pxml"
        Dim target As String
        Dim outlookBusy As Boolean = False

        For Each regel In Form1.tbPersonalXML.Lines


            If regel.StartsWith("<Blok>") Then
                blokRead = True
            End If

            If blokRead = True Then

            End If

            If blokRead Then
                tempText = tempText + regel + vbNewLine
            End If

            If regel.StartsWith("</Blok>") Then
                blokRead = False
                target = tempTarget + teller.ToString + ".xml"
                FileHandlerClass.saveFile(tempText, target) 'locatie
                teller = teller + 1
                tempText = ""


                toOutlook(target)
            End If

        Next

    End Sub

    Dim tempVak As String
    Dim tempDocent As String
    Dim tempKlas As String
    Dim tempLokaal As String
    Dim tempWeken() As String
    Dim startDatum As Date

    Public Sub toOutlook(ByVal target As String)


        'Open the XML.
        docNav = New XPathDocument(target)



        'Create a navigator to query with XPath.
        docNavigator = docNav.CreateNavigator

        'This expression uses standard XPath syntax.
        Docent = "(/Blok/Docent)"
        Dag = "(/Blok/Dag)"
        Klas = "(/Blok/Klas)"
        Vak = "(/Blok/Vak)"
        Lokaal = "(/Blok/Lokaal)"
        Lesuur = "(/Blok/Lesuur)"
        Weken = "(/Blok/Weken)"


        'Select the node and put the results into an iterator.
        nodeDocent = docNavigator.Select(Docent)
        nodeDag = docNavigator.Select(Dag)
        nodeKlas = docNavigator.Select(Klas)
        nodeVak = docNavigator.Select(Vak)
        nodeLokaal = docNavigator.Select(Lokaal)
        nodeLesuur = docNavigator.Select(Lesuur)
        nodeWeken = docNavigator.Select(Weken)



        While (nodeDocent.MoveNext())
            If tempDocent = "" Then
                tempDocent = nodeDocent.Current.Value
            Else
                tempDocent = tempDocent + " en " + nodeDocent.Current.Value
            End If
        End While

        While (nodeKlas.MoveNext())
            If tempKlas = "" Then
                tempKlas = nodeKlas.Current.Value
            Else
                tempKlas = tempKlas + " en " + nodeKlas.Current.Value
            End If
        End While

        While (nodeLokaal.MoveNext())
            If tempLokaal = "" Then
                tempLokaal = nodeLokaal.Current.Value
            Else
                tempLokaal = tempLokaal + " en " + nodeLokaal.Current.Value
            End If
        End While

        While (nodeVak.MoveNext())
            tempVak = nodeVak.Current.Value
        End While

        While (nodeWeken.MoveNext())
            Dim temp As String
            If nodeWeken.Current.Value = "" Then
                temp = "1,2,3,4,5,6,7,8,9,10"
            Else
                temp = nodeWeken.Current.Value
            End If
            tempWeken = Split(temp, ",")
        End While

        For i = 0 To nodeDag.Count - 1

            While (nodeDag.MoveNext) And (nodeLesuur.MoveNext)



                For y = 0 To tempWeken.Length - 1


                    startDatum = Form1.tbStartDatum.Text

                    If tempWeken(y) = "" Then
                        tempWeken(y) = 0
                    End If
                    startDatum = startDatum.AddDays(((Int(tempWeken(y)) * 7) + (nodeDag.Current.Value - 1)))


                    Dim starttijd As Date
                    Dim eindtijd As Date
                    Dim oApp As Outlook.Application = New Outlook.Application()
                    Dim oAppt As Outlook.AppointmentItem = oApp.CreateItem(Outlook.OlItemType.olAppointmentItem)

                    starttijd = convertUur(nodeLesuur.Current.Value)
                    eindtijd = CDate(DateAdd(DateInterval.Minute, 50, starttijd).ToLongTimeString())

                    oAppt.Subject = tempVak
                    oAppt.Body = "Deze les wordt gegeven door " & tempDocent & " aan " & tempKlas
                    oAppt.Location = tempLokaal
                    oAppt.Start = startDatum + " " + starttijd
                    oAppt.End = startDatum + " " + eindtijd
                    oAppt.ReminderSet = True
                    oAppt.ReminderMinutesBeforeStart = 5
                    oAppt.BusyStatus = Outlook.OlBusyStatus.olBusy
                    oAppt.IsOnlineMeeting = False
                    oAppt.Categories = "Rooster"
                    ' zelf in outlook een kleurtje aangeven

                    oAppt.Save()

                    oApp = Nothing
                    oAppt = Nothing
                Next
            End While
        Next


        If System.IO.File.Exists(target) = True Then
            System.IO.File.Delete(target)
        End If




    End Sub

    Public Function convertUur(ByVal uur As Integer)

        Dim uren As Date

        Dim beginUur As Date = CDate("08:45:00 AM")
        Dim LesroosterUren As Integer = uur
        'Omzetten van lesuren naar tijd

        Select Case LesroosterUren
            Case 1
                uren = beginUur
            Case 2
                uren = CDate(DateAdd(DateInterval.Minute, 50, beginUur).ToLongTimeString())
            Case 3
                uren = CDate(DateAdd(DateInterval.Minute, 120, beginUur).ToLongTimeString())
            Case 4
                uren = CDate(DateAdd(DateInterval.Minute, 170, beginUur).ToLongTimeString())
            Case 5
                uren = CDate(DateAdd(DateInterval.Minute, 220, beginUur).ToLongTimeString())
            Case 6
                uren = CDate(DateAdd(DateInterval.Minute, 270, beginUur).ToLongTimeString())
            Case 7
                uren = CDate(DateAdd(DateInterval.Minute, 320, beginUur).ToLongTimeString())
            Case 8
                uren = CDate(DateAdd(DateInterval.Minute, 390, beginUur).ToLongTimeString())
            Case 9
                uren = CDate(DateAdd(DateInterval.Minute, 440, beginUur).ToLongTimeString())
            Case 10
                uren = CDate(DateAdd(DateInterval.Minute, 490, beginUur).ToLongTimeString())
            Case 11
                uren = CDate(DateAdd(DateInterval.Minute, 555, beginUur).ToLongTimeString())
            Case 12
                uren = CDate(DateAdd(DateInterval.Minute, 605, beginUur).ToLongTimeString())
            Case 13
                uren = CDate(DateAdd(DateInterval.Minute, 675, beginUur).ToLongTimeString())
            Case 14
                uren = CDate(DateAdd(DateInterval.Minute, 725, beginUur).ToLongTimeString())
        End Select

        Return uren

    End Function


End Class
